/**
 * File Info：com.softisland.config.common.BaseEntity.java
 * Created Date：2017年4月28日 下午1:12:53
 * Created User: mys
 */
package com.mys.springboot.config.base;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

/**
 * @description
 * 
 * 				基础实体对象
 * @author mys
 * @version 1.0
 *
 */
@SuppressWarnings("unchecked")
public class BaseEntity extends HashMap<Object, Object> implements Map<Object, Object> {
	private static final long serialVersionUID = 485257446018878220L;

	/**
	 * 构造器
	 */
	public BaseEntity() {

	}

	/**
	 * 构造器
	 * 
	 * @param request
	 */
	public BaseEntity(HttpServletRequest request) {
		Map<?, ?> properties = request.getParameterMap();
		Iterator<?> entries = properties.entrySet().iterator();
		Map.Entry<?, ?> entry;
		String name = "";
		String value = "";
		while (entries.hasNext()) {
			entry = (Map.Entry<?, ?>) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				for (int i = 0; i < values.length; i++) {
					value = values[i] + ",";
				}
				value = value.substring(0, value.length() - 1);
			} else {
				value = valueObj.toString();
			}
			this.put(name, value);
		}
	}

	public <W> W get(Object key, Class<W> clz) {
		return (W) this.get(key);
	}

	public String getString(Object key) {
		return this.get(key, String.class);
	}

	public Boolean getBoolean(Object key) {
		return this.get(key, Boolean.class);
	}

	public Integer getInteger(Object key) {
		return this.get(key, Integer.class);
	}

	public Float getFloat(Object key) {
		return this.get(key, Float.class);
	}

	public Long getLong(Object key) {
		return this.get(key, Long.class);
	}

	public Double getDouble(Object key) {
		return this.get(key, Double.class);
	}

}
